<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script>
        var arr = [1, 2, 3, 2, 2, 3, 2, 2, 2, 2];
        var sy2 = arr.indexOf(2);
        var num = 0;
        while (sy2 !== -1) {
            console.log(sy2);
            num++;
            sy2 = arr.indexOf(2, sy2 + 1);
        }
        console.log('2出现的次数是' + num + '次');
    </script>
</head>

<body>

</body>

</html>